<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="800px"
    :before-close="handleClose"
  >
    <el-form ref="formAdd" :model="form" :rules="rules" label-width="140px">
      <el-form-item label="文章ID" prop="article_id">
        <el-input
          v-model="form.article_id"
          size="mini"
          clearable
          placeholder="请输入文章ID"
        ></el-input>
      </el-form-item>
      <el-form-item label="文章标题" prop="article_title">
        <el-input
          v-model="form.article_title"
          size="mini"
          clearable
          placeholder="请输入文章标题"
        ></el-input>
      </el-form-item>
      <el-form-item label="评论来源" prop="from_username">
        <el-input
          v-model="form.from_username"
          size="mini"
          clearable
          placeholder="请输入评论来源"
        ></el-input>
      </el-form-item>
      <el-form-item label="来源评论用户头像" prop="from_user_logo">
        <el-input
          v-model="form.from_user_logo"
          size="mini"
          clearable
          placeholder="请输入来源评论用户头像"
        ></el-input>
      </el-form-item>
      <el-form-item label="回复评论名称" prop="to_username">
        <el-input
          v-model="form.to_username"
          size="mini"
          clearable
          placeholder="请输入回复评论名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="回复评论用户头像" prop="to_user_logo">
        <el-input
          v-model="form.to_user_logo"
          size="mini"
          clearable
          placeholder="请输入回复评论用户头像"
        ></el-input>
      </el-form-item>
      <el-form-item label="回复评论内容" prop="content">
        <el-input
          v-model="form.content"
          size="mini"
          clearable
          placeholder="请输入回复评论内容"
        ></el-input>
      </el-form-item>
      <el-divider />
      <mk-get-button @save="save" @close="handleClose" />
    </el-form>
  </el-dialog>
</template>

<script>
import MkGetButton from "@/components/common/mk-get-button";
import MkBackList from "@/components/common/mk-back-list";

import {
  dirCommentsCreate,
  dirCommentsUpdate,
} from "@/api/modules/sys.comments.api";

export default {
  name: "get-comments-info",
  components: { MkBackList, MkGetButton },
  props: {
    updateData: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      dialogTitle: "",
      type: null,
      dialogVisible: false,
      commonKey: 0,
      form: {},
      isAdd: true,
      rules: {
        article_id: [
          { required: true, message: "请输入文章ID", trigger: "blur" },
        ],

        article_title: [
          { required: true, message: "请输入文章标题", trigger: "blur" },
        ],

        content: [
          { required: true, message: "请输入回复评论内容", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    open(type, typeName, data) {
      this.resetForm();
      this.type = type;
      this.dialogTitle = typeName;
      this.dialogVisible = true;
      if (data) {
        this.form = data;
      }
    },
    save() {
      this.$refs["formAdd"].validate((valid) => {
        if (valid) {
          let _FUC = "";
          _FUC = this.type == "create" ? dirCommentsCreate : dirCommentsUpdate;
          _FUC(this.form)
            .then((res) => {
              this.$toast.success(
                this.type == "create" ? "添加成功！" : "修改成功！"
              );
              this.dialogVisible = false;
              this.$emit("success");
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.dialogVisible = false;
        })
        .catch((_) => {});
    },
    resetForm() {
      this.form = {};
    },
  },
};
</script>

<style lang="scss" scoped></style>
